<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/22
 * Time: 15:14
 */

namespace app\admin\controller;


use app\api\controller\Jpushmember;
use app\common\controller\Adminbase;
use think\Db;

class Applyhelp extends Adminbase
{
    /**
     * 申请帮扶列表(普通)
     * */
    public function index()
    {

        if ($this->request->isAjax()) {
            $order = "level desc,create_time desc";
            $limit = $this->request->param('limit/d', 10);
            $page = $this->request->param('page/d', 10);

            $status = input('status','');
            if($status){
                if($status == -2){
                    $status = 0;
                }
                $where[] = ['help_list.status','=',$status];
            }

            $where[] = ['help_list.level','=',1];
            $mobile = input('mobile');
            if($mobile){
                $where[] = ['help_list.mobile|member.username','like','%'.$mobile.'%'];
            }
            $time = input('time');
            if($time){
                $start_time = substr($time, 0, 20);//订单那创建开始时间
                $end_time = substr($time, 21, 20);//订单那创建开始时间
                $time = strtotime($start_time);
                $times = strtotime($end_time);
                $where[] = ['help_list.create_time','between',$time.','.$times];
            }

            $list = Db::view('help_list','*')
                ->view('member','username','member.id = help_list.user_id')
                ->view('member a','username as eName','a.id = help_list.pass_user','left')
                ->view('images b','img as id_img_up','b.id = help_list.id_img_up','left')
                ->view('images c','img as id_img_down','c.id = help_list.id_img_down','left')
                ->where($where)
                ->page($page, $limit)
                ->order($order)
                ->withAttr('create_time', function ($value, $data) {
                    return date('Y-m-d H:i:s', $value);
                })
                ->withAttr('pass_time', function ($value, $data) {
                    if(empty($value)){
                        return "--";
                    }else{
                        return date('Y-m-d H:i:s', $value);
                    }
                })
                ->withAttr('finish_time', function ($value, $data) {
                    if(empty($value)){
                        return "--";
                    }else {
                        return date('Y-m-d H:i:s', $value);
                    }
                })
                ->withAttr('level', function ($value, $data) {
                    if($value == 1){
                        return "普通";
                    }else{
                        return "紧急";
                    }
                })
                ->withAttr('eName', function ($value, $data) {
                    if($value){
                        return $value;
                    }else{
                        return "--";
                    }
                })
                ->withAttr('is_dang', function ($value, $data) {
                    if($value == 0){
                        return "否";
                    }else{
                        return "是";
                    }
                })
                ->withAttr('id_img_up', function ($value, $data) {
                    if($value == 0){
                        return $value;
                    }else{
                        return "";
                    }
                })
                ->withAttr('id_img_down', function ($value, $data) {
                    if($value == 0){
                        return $value;
                    }else{
                        return "";
                    }
                })
                ->select();
            foreach ($list as $k => $v){
                $role = Db::name('member')->where(['id' => $v['help_id']])->value('role_id');
                if($v['status'] == 1){
                    $list[$k]['status_name'] = "审核通过(二级管理)";
                }else if($v['status'] == 2){
                    $list[$k]['status_name'] = "审核通过(一级管理)";
                }else if($v['status'] == 3){
                    $list[$k]['status_name'] =  "待完成";
                }else if($v['status'] == 0){
                    $list[$k]['status_name'] =  "待帮扶";
                }else if($v['status'] == -1){
                    if($role == 2){
                        $list[$k]['status_name'] =  "已拒绝(二级管理)";
                    }else{
                        $list[$k]['status_name'] =  "已拒绝(一级管理)";
                    }
                }else if($v['status'] == 4){
                    $list[$k]['status_name'] =  "已完成";
                }else if($v['status'] == 5){
                    $list[$k]['status_name'] =  "已归档";
                }
            }

            $total = Db::view('help_list','*')
                ->view('member','username','member.id = help_list.user_id')
                ->view('member a','username as eName','a.id = help_list.pass_user','left')
                ->where($where)->count();
            $result = array("code" => 0, "count" => $total, "data" => $list);
            return json($result);
        }
        return $this->fetch();

    }


    /**
     * 申请帮扶列表(紧急)
     * */
    public function indexUrgent()
    {

        if ($this->request->isAjax()) {
            $order = "level desc,create_time desc";
            $limit = $this->request->param('limit/d', 10);
            $page = $this->request->param('page/d', 10);

            $status = input('status','');
            if($status){
                if($status == -2){
                    $status = 0;
                }
                $where[] = ['help_list.status','=',$status];
            }

            $where[] = ['help_list.level','=',2];
            $mobile = input('mobile');
            if($mobile){
                $where[] = ['help_list.mobile|member.username','like','%'.$mobile.'%'];
            }
            $time = input('time');
            if($time){
                $start_time = substr($time, 0, 20);//订单那创建开始时间
                $end_time = substr($time, 21, 20);//订单那创建开始时间
                $time = strtotime($start_time);
                $times = strtotime($end_time);
                $where[] = ['help_list.create_time','between',$time.','.$times];
            }

            $list = Db::view('help_list','*')
                ->view('member','username','member.id = help_list.user_id')
                ->view('member a','username as eName','a.id = help_list.pass_user','left')
                ->view('images b','img as id_img_up','b.id = help_list.id_img_up','left')
                ->view('images c','img as id_img_down','c.id = help_list.id_img_down','left')
//                ->view('member b','username as hName','b.id = help_list.help_id','left')
                ->where($where)
                ->page($page, $limit)
                ->order($order)
                ->withAttr('create_time', function ($value, $data) {
                    return date('Y-m-d H:i:s', $value);
                })
                ->withAttr('pass_time', function ($value, $data) {
                    if(empty($value)){
                        return "--";
                    }else{
                        return date('Y-m-d H:i:s', $value);
                    }
                })
                ->withAttr('finish_time', function ($value, $data) {
                    if(empty($value)){
                        return "--";
                    }else {
                        return date('Y-m-d H:i:s', $value);
                    }
                })
                ->withAttr('level', function ($value, $data) {
                    if($value == 1){
                        return "普通";
                    }else{
                        return "紧急";
                    }
                })
                ->withAttr('sex', function ($value, $data) {
                    if($value == 1){
                        return "男";
                    }else{
                        return "女";
                    }
                })
                ->withAttr('eName', function ($value, $data) {
                    if($value){
                        return $value;
                    }else{
                        return "--";
                    }
                })
                ->withAttr('is_dang', function ($value, $data) {
                    if($value == 0){
                        return "否";
                    }else{
                        return "是";
                    }
                })
                ->withAttr('id_img_up', function ($value, $data) {
                    if($value == 0){
                        return $value;
                    }else{
                        return "";
                    }
                })
                ->withAttr('id_img_down', function ($value, $data) {
                    if($value == 0){
                        return $value;
                    }else{
                        return "";
                    }
                })
                ->select();

            foreach ($list as $k => $v){
                if($v['pass_user'] == $v['help_id']){
                    $list[$k]['hName'] = "--";
                }else{
                    $list[$k]['hName'] = Db::name('member')->where(['id' => $v['help_id']])->value('username');
                }
                if($v['status'] == 1){
                    $list[$k]['status_name'] = "待帮扶";
                }else if($v['status'] == 2){
                    $list[$k]['status_name'] = "已帮扶";
                }else if($v['status'] == 3){
                    $list[$k]['status_name'] =  "已完成";
                }else if($v['status'] == 0){
                    $list[$k]['status_name'] =  "未审核";
                }else if($v['status'] == -1){
                    $list[$k]['status_name'] =  "未帮扶";
                }else if($v['status'] == 4){
                    $list[$k]['status_name'] =  "已归档";
                }
            }

            $total = Db::view('help_list','*')
                ->view('member','username','member.id = help_list.user_id')
                ->view('member a','username as eName','a.id = help_list.pass_user','left')
//                ->view('member b','username as hName','b.id = help_list.help_id','left')
                ->where($where)->count();
            $result = array("code" => 0, "count" => $total, "data" => $list);
            return json($result);
        }
        return $this->fetch();

    }



    /**
     * 申请帮扶列表详情
     * */
    public function info()
    {

        if ($this->request->isAjax()) {
            $id = input('id');
            if (!$id) {
                $result = array("code" => -1, "msg" => "数据获取有误", "data" => []);
            } else {
                $info = Db::name('help_list')
                    ->where(['id' => $id])
                    ->find();
                $upImg = Db::name('images')->where(['id' => $info['id_img_up']])->value('img');
                $downImg = Db::name('images')->where(['id' => $info['id_img_down']])->value('img');
                $info['upImg'] = $upImg;
                $info['downImg'] = $downImg;
                //补充材料数组
                $arrB = [];
                foreach ($info['buchong'] as $k => $v){
                    $img = Db::name('images')->where(['id' => $v])->value('img');
                    array_push($arrB,$img);
                }
                $info['buchong'] = $arrB;
                $result = array("code" => 1, "msg" => "获取成功", "data" => $info);
            }
            return json($result);
        }else{
            $id = $this->request->param('id/d');
            $where[] = ['help_list.id','=',$id];
            $info = Db::view('help_list','*')
                ->view('member','username','member.id = help_list.user_id')
                ->view('member a','username as eName','a.id = help_list.pass_user','left')
                ->where($where)
                ->withAttr('create_time', function ($value, $data) {
                    return date('Y-m-d H:i:s', $value);
                })
                ->withAttr('pass_time', function ($value, $data) {
                    if(empty($value)){
                        return "--";
                    }else{
                        return date('Y-m-d H:i:s', $value);
                    }
                })
                ->withAttr('finish_time', function ($value, $data) {
                    if(empty($value)){
                        return "--";
                    }else {
                        return date('Y-m-d H:i:s', $value);
                    }
                })
                ->withAttr('level', function ($value, $data) {
                    if($value == 1){
                        return "普通";
                    }else{
                        return "紧急";
                    }
                })
                ->withAttr('eName', function ($value, $data) {
                    if($value){
                        return $value;
                    }else{
                        return "--";
                    }
                })
                ->withAttr('is_dang', function ($value, $data) {
                    if($value == 0){
                        return "否";
                    }else{
                        return "是";
                    }
                })
                ->find();

            if($info['status'] == 1){
                $info['status_name'] = "待帮扶";
            }else if($info['status'] == 2){
                $info['status_name'] = "待帮扶";
            }else if($info['status'] == 3){
                $info['status_name'] =  "已帮扶";
            }else if($info['status'] == 0){
                $info['status_name'] =  "未审核";
            }else if($info['status'] == -1){
                $info['status_name'] =  "未帮扶";
            }else if($info['status'] == 4){
                $info['status_name'] =  "已归档";
            }

            $upImg = Db::name('images')->where(['id' => $info['id_img_up']])->value('img');
            $downImg = Db::name('images')->where(['id' => $info['id_img_down']])->value('img');
            $info['upImg'] = $upImg;
            $info['downImg'] = $downImg;
            //补充材料数组
            $arrB = [];
			 if(!empty($info['buchong'])){
                $buchong = explode(',',$info['buchong']);
                foreach ($buchong as $k => $v){
                    $img = Db::name('images')->where(['id' => $v])->value('img');
                    array_push($arrB,$img);
                }
            }
            $info['buchong'] = $arrB;
            if (empty($info)) {
                $this->error('该信息不存在！');
            }
            $this->assign("data", $info);
            return $this->fetch();
        }

    }

    /**
     * 帮扶通过
     * */
    public function makeSure()
    {

        $id = input('id');
        if ($this->request->isAjax()) {

            $beizhu = input('beizhu');

            if(!$id || !$beizhu){
                $result = array("code" => -1, "msg" => "数据获取有误", "data" => []);
                return json($result);
            }

            $data['pass_msg'] = $beizhu;
            $data['status'] = 4;
            $data['pass_time'] = time();

            $res = Db::name('help_list')->where(['id' => $id])->update($data);

            if($res){

                $user = [];

                $info = Db::name('help_list')->where(['id' => $id])->field('user_id,pass_msg')->find();
                array_push($user,$info['user_id']);
                $jPush = new Jpushmember();
                $content = "您的帮扶申请已经通过";
                $result = $jPush->pushAlias($user,$content);

                if($result['http_code'] == 200){
                    $maps[] = ['apply_id','=',$id];
                    $maps[] = ['type','=',1];
                    $helpCount = Db::name('send_msg')->where($maps)->find();
                    if(count($helpCount)){
                        $upData['is_check'] = 0;
                        $reson = Db::name('send_msg')->where(['id' => $helpCount['id']])->update($upData);
                    }else{

                        $inData = [
                            'user_id' => $info['user_id'],
                            'apply_id' => $id,
                            'title' => '您的申请有了新进度',
                            'content' => $info['pass_msg'],
                            'create_time' => time(),
                            'is_check' => 0,
                            'type' => 1
                        ];
                        $reson = Db::name('send_msg')->where(['id' => $helpCount['id']])->insert($inData);
                    }
                }

                $result = array("code" => 1, "msg" => "操作成功", "data" => [],"url" => "/admin/applyhelp/index");
            }else{
                $result = array("code" => -1, "msg" => "操作失败", "data" => []);
            }
            return json($result);
        }else{
            $data['id'] = $id;
            $this->assign('data',$data);
            return $this->fetch();
        }

    }

    /**
     * 帮扶审核拒绝
     * */
    public function fail2()
    {

        $id = input('id');
        if(!$id){
            $result = array("code" => -1, "msg" => "数据获取有误", "data" => []);
            return json($result);
        }
        $data['status'] = -1;
        $data['pass_time'] = time();
        $res = Db::name('help_list')->where(['id' => $id])->update($data);

        if($res){
            $result = array("code" => 1, "msg" => "操作成功", "data" => [],"url" => "/admin/applyhelp/index");
        }else{
            $result = array("code" => -1, "msg" => "操作失败", "data" => []);
        }
        return json($result);

    }

    /**
     * 帮扶审核拒绝
     * */
    public function fail()
    {

        $id = input('id');
        if(!$id){
            $this->error('获取数据有误');
            //$result = array("code" => -1, "msg" => "数据获取有误", "data" => []);
            //return json($result);
        }
        if($this->request->isPost()){

            $id = input('id','');
			
            $type = Db::name('help_list')->where(['id' => $id])->value('type');
            if($type == 1){
                $url = "/admin/applyhelp/index";
            }else{
                $url = "/admin/applyhelp/index_urgent";
            }
            $beizhu = input('beizhu','');
            if(!$beizhu){
                $result = array("code" => -1, "msg" => "备注不能为空", "data" => []);
                return json($result);
            }
            $data['beizhu'] = $beizhu;
            $data['status'] = -1;
            $data['pass_time'] = time();
            $res = Db::name('help_list')->where(['id' => $id])->update($data);

            if($res){

                $user = [];

                $info = Db::name('help_list')->where(['id' => $id])->field('user_id,pass_msg')->find();
                array_push($user,$info['user_id']);
                $jPush = new Jpushmember();

                $content = "您的帮扶申请被拒绝：".$beizhu;
                $result = $jPush->pushAlias($user,$content);

                if($result['http_code'] == 200){
                    $maps[] = ['apply_id','=',$id];
                    $maps[] = ['type','=',1];
                    $helpCount = Db::name('send_msg')->where($maps)->find();
                    if(count($helpCount)){
                        $upData['is_check'] = 0;
                        $reson = Db::name('send_msg')->where(['id' => $helpCount['id']])->update($upData);
                    }else{

                        $inData = [
                            'user_id' => $info['user_id'],
                            'apply_id' => $id,
                            'title' => '您的申请有了新进度',
                            'content' => $beizhu,
                            'create_time' => time(),
                            'is_check' => 0,
                            'type' => 1
                        ];
                        $reson = Db::name('send_msg')->where(['id' => $helpCount['id']])->insert($inData);
                    }
                }

                //$this->success('操作成功');
                $result = array("code" => 1, "msg" => "操作成功", "data" => [],"url" => $url);
            }else{
                //$this->error('操作失败');
                $result = array("code" => -1, "msg" => "操作失败", "data" => [],"url" => $url);
            }
			return json($result);
        }else{
            $this->assign('id',$id);
            return $this->fetch();
        }

    }


    /**
     * 指派帮助人
     * */
    public function helpMember()
    {
        if($this->request->isPost()){
            $user_id = input('user_id');
            $id = input('id');

            if(!$user_id || !$id){
                $this->error('数据获取有误!');
            }

            $data['help_id'] = $user_id;
            $data['pass_user'] = $user_id;
            $data['status'] = 1;
            $res = Db::name('help_list')->where(['id' => $id])->update($data);

            $res != false ? true:false;
            if($res){

                $jPush = new Jpushmember();

                $user = [];

                $info = Db::name('help_list')->where(['id' => $id])->field('user_id,pass_msg')->find();
                array_push($user,$info['user_id']);
                $content = "您的申请有了新的进度";
                $result = $jPush->pushAlias($user,$content);

                if($result['http_code'] == 200){
                    $maps[] = ['apply_id','=',$id];
                    $maps[] = ['type','=',1];
                    $helpCount = Db::name('send_msg')->where($maps)->find();
                    if(count($helpCount)){
                        $upData['is_check'] = 0;
                        $reson = Db::name('send_msg')->where(['id' => $helpCount['id']])->update($upData);
                    }else{

                        $inData = [
                            'user_id' => $info['user_id'],
                            'apply_id' => $id,
                            'title' => '您的申请有了新进度',
                            'content' => $info['pass_msg'],
                            'create_time' => time(),
                            'is_check' => 0,
                            'type' => 2
                        ];
                        $reson = Db::name('send_msg')->where(['id' => $helpCount['id']])->insert($inData);
                    }
                }

                $this->success('指派成功');
            }else{
                $this->error('操作有误');
            }

        }else{
            $id = $this->request->param('id');
            $apply = Db::name('help_list')->where(['id' => $id])->field('user_id,danwei,zhibu')->find();
            $map[] = ['status','=','1'];
            $map[] = ['id','<>',$apply['user_id']];
            $map[] = ['role_id','=',3];
            $map[] = ['danwei','=',$apply['danwei']];
            $map[] = ['zhibu','=',$apply['zhibu']];

            $list = Db::name('member')->where($map)->field('username,id')->select();
			
			if(count($list) == 0){
                $maps[] = ['status','=','1'];
                $maps[] = ['id','<>',$apply['user_id']];
                $maps[] = ['role_id','=',3];
                $list = Db::name('member')->where($maps)->field('username,id')->select();
            }
			
            $this->assign('data',$list);
            $this->assign('id',$id);
            return $this->fetch();

        }
    }


    /**
     * 筛选用户
     * */
    public function chooseUser()
    {

        $address = input('address','');
        if($address){
            $where[] = ['danwei','like','%'.$address.'%'];
        }
        $where[] = ['status','=','1'];
        $where[] = ['role_id','=',3];

        $list = Db::name('member')->where($where)->field('username,id')->select();

        $result = array("code" => 1, "msg" => "获取成功", "data" => $list);

        return json($result);

    }

    /**
     * 完成归档操作
     * */
    public function finish()
    {

        $id = input('id');

        $userId = Db::name('help_list')->where(['id' => $id])->field('user_id,role_bei,level')->find();
        $user = [];
        array_push($user,$userId['user_id']);
		if($userId['level'] == 1){
			$data['status'] = 5;
		}else{
			$data['status'] = 4;
		}

        $res = Db::name('help_list')->where(['id' => $id])->update($data);

        $jPush = new Jpushmember();

        $content = "您的帮扶申请已经完成";
        $jPush->pushAlias($user,$content);

        //查询是否由该条信息
        $msgMap[] = ['user_id','=',$userId['user_id']];
        $msgMap[] = ['apply_id','=',$id];
        $msgMap[] = ['type','=',1];
        $msgC = Db::name('send_msg')->where($msgMap)->find();
        if(count($msgC)){
            if($msgC['is_check'] == 1){
                $msgData['is_check'] = 0;
                Db::name('send_msg')->where(['id' => $id])->update($msgData);
            }
        }else{
            $msgData = [
                'user_id' => $userId['user_id'],
                'apply_id' => $id,
                'title' => "您的帮扶申请已经完成",
                'content' => $userId['role_bei'],
                'create_time' => time(),
                'is_check' => 0,
                'type' => 2
            ];
            Db::name('send_msg')->insert($msgData);
        }

        if($res){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }

    }


    /**
     * 帮扶导出功能
     * */
    public function export()
    {

        $type = input('type',1);
        $time = input('time','');
        $user = input('mobile','');
        $status = input('status','');
        $where[] = ['help_list.level','=',$type];

        if($time){
            $start_time = substr($time, 0, 20);//订单那创建开始时间
            $end_time = substr($time, 21, 20);//订单那创建开始时间
            $time = strtotime($start_time);
            $times = strtotime($end_time);
            $where[] = ['help_list.create_time','between',$time.','.$times];
        }

        if($user){
            $where[] = ['help_list.mobile|member.username','like','%'.$user.'%'];
        }

        if($status){
                if($status == -2){
                    $status = 0;
                }
                $where[] = ['help_list.status','=',$status];
            }

        $order = "create_time desc";

        $data = Db::view('help_list','*')
            ->view('member','username','member.id = help_list.user_id')
            ->view('member a','username as eName','a.id = help_list.pass_user','left')
            ->view('images b','img as id_img_up','b.id = help_list.id_img_up','left')
            ->view('images c','img as id_img_down','c.id = help_list.id_img_down','left')
//                ->view('member b','username as hName','b.id = help_list.help_id','left')
            ->where($where)
            ->order($order)
            ->withAttr('create_time', function ($value, $data) {
                return date('Y-m-d H:i:s', $value);
            })
            ->withAttr('pass_time', function ($value, $data) {
                if(empty($value)){
                    return "--";
                }else{
                    return date('Y-m-d H:i:s', $value);
                }
            })
            ->withAttr('finish_time', function ($value, $data) {
                if(empty($value)){
                    return "--";
                }else {
                    return date('Y-m-d H:i:s', $value);
                }
            })
            ->withAttr('level', function ($value, $data) {
                if($value == 1){
                    return "普通";
                }else{
                    return "紧急";
                }
            })
            ->withAttr('sex', function ($value, $data) {
                if($value == 1){
                    return "男";
                }else{
                    return "女";
                }
            })
            ->withAttr('eName', function ($value, $data) {
                if($value){
                    return $value;
                }else{
                    return "--";
                }
            })
            ->withAttr('is_dang', function ($value, $data) {
                if($value == 0){
                    return "否";
                }else{
                    return "是";
                }
            })
//            ->withAttr('id_img_up', function ($value, $data) {
//                if($value == 0){
//                    return "http://www.bitxiaoxi.com/".$value;
//                }else{
//                    return "";
//                }
//            })
//            ->withAttr('id_img_down', function ($value, $data) {
//                if($value == 0){
//                    return "http://www.bitxiaoxi.com/".$value;
//                }else{
//                    return "";
//                }
//            })
            ->select();

        foreach ($data as $k => $v){
            if($v['pass_user'] == $v['help_id']){
                $data[$k]['hName'] = "--";
            }else{
                $data[$k]['hName'] = Db::name('member')->where(['id' => $v['help_id']])->value('username');
            }
            if($v['status'] == 1){
                $data[$k]['status_name'] = "待帮扶";
            }else if($v['status'] == 2){
                $data[$k]['status_name'] = "已帮扶";
            }else if($v['status'] == 3){
                $data[$k]['status_name'] =  "已完成";
            }else if($v['status'] == 0){
                $data[$k]['status_name'] =  "未审核";
            }else if($v['status'] == -1){
                $data[$k]['status_name'] =  "未帮扶";
            }else if($v['status'] == 4){
                $data[$k]['status_name'] =  "已归档";
            }
        }


        $objPHPExcel = new \PHPExcel();

        $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);

        $objActSheet = $objPHPExcel->getActiveSheet();

        // 水平居中（位置很重要，建议在最初始位置）
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('I')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('J')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('K')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('L')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('M')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('N')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('O')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('P')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('Q')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('R')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('S')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('T')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('U')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('V')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('W')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('X')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('Y')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('Z')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('AA')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('AB')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('AC')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('AD')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

        $objActSheet->setCellValue('A1', 'ID');
        $objActSheet->setCellValue('B1', '等级');
        $objActSheet->setCellValue('C1', '申请人账号');
        $objActSheet->setCellValue('D1', '是否党员');
        $objActSheet->setCellValue('E1', '帮扶状态');
        $objActSheet->setCellValue('F1', '审核人');
        $objActSheet->setCellValue('G1', '类型');
        $objActSheet->setCellValue('H1', '姓名');
        $objActSheet->setCellValue('I1', '民族');
        $objActSheet->setCellValue('J1', '性别');
        $objActSheet->setCellValue('K1', '出生日期');
        $objActSheet->setCellValue('L1', '手机号');
        $objActSheet->setCellValue('M1', '所属单位');
        $objActSheet->setCellValue('N1', '所属支部');
        $objActSheet->setCellValue('O1', '工作单位地址');
        $objActSheet->setCellValue('P1', '工作单位地址详情');
        $objActSheet->setCellValue('Q1', '入党时间');
        $objActSheet->setCellValue('R1', '基本情况');
        $objActSheet->setCellValue('S1', '基本情况详情');
        $objActSheet->setCellValue('T1', '申请时间');
        $objActSheet->setCellValue('U1', '审核时间');
        $objActSheet->setCellValue('V1', '完成时间');
        $objActSheet->setCellValue('W1', '身份证正面照');
        $objActSheet->setCellValue('X1', '身份证反面照');
        $objActSheet->setCellValue('Y1', '补充材料');
        // 设置个表格宽度
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(16);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('M')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('N')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('O')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('P')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('Q')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('R')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('S')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('T')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('U')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('V')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('W')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('X')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('Y')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('Z')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('AA')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('AB')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('AC')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('AD')->setWidth(20);

        // 垂直居中
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('I')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('J')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('K')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('L')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('M')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('N')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('O')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('P')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('Q')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('R')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('S')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('T')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('U')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('V')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('W')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('X')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('Y')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('Z')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('AA')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('AB')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('AC')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('AD')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $cellName=array('A','B','C','D','E','F','G','H','I','J','K','L','M','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD');
        foreach($data as $k=>$v){
            $k +=2;
            $objActSheet->setCellValue('A'.$k, $v['id']);
            $objActSheet->setCellValue('B'.$k, $v['level']);
            $objActSheet->setCellValue('C'.$k, $v['username']);
            $objActSheet->setCellValue('D'.$k, $v['is_dang']);
            $objActSheet->setCellValue('E'.$k, $v['status_name']);
            $objActSheet->setCellValue('F'.$k, $v['eName']);
            $objActSheet->setCellValue('G'.$k, $v['type']);
            $objActSheet->setCellValue('H'.$k, $v['name']);
            $objActSheet->setCellValue('I'.$k, $v['nation']);

            // 表格内容
            $objActSheet->setCellValue('J'.$k, $v['sex']);
            $objActSheet->setCellValue('K'.$k, $v['born_date']);
            $objActSheet->setCellValue('L'.$k, $v['mobile']);
            $objActSheet->setCellValue('M'.$k, $v['danwei']);
            $objActSheet->setCellValue('N'.$k, $v['zhibu']);
            $objActSheet->setCellValue('O'.$k, $v['work_address']);
            $objActSheet->setCellValue('P'.$k, $v['work_address_info']);
            $objActSheet->setCellValue('Q'.$k, $v['rudang_date']);
            $objActSheet->setCellValue('R'.$k, $v['msg']);
            $objActSheet->setCellValue('S'.$k, $v['msg_info']);
            $objActSheet->setCellValue('T'.$k, $v['create_time']);
            $objActSheet->setCellValue('U'.$k, $v['pass_time']);
            $objActSheet->setCellValue('V'.$k, $v['finish_time']);


            // 图片生成new \PHPExcel_Worksheet_Drawing();
//            halt('.'.$v['id_img_up']);

            if($v['id_img_up']){
                $objDrawing[$k] = new \PHPExcel_Worksheet_Drawing();

                $objDrawing[$k]->setPath('.'.$v['id_img_up']);//这里拼接 . 是因为要在根目录下获取
                // 设置宽度高度
                $objDrawing[$k]->setHeight(80);//照片高度
                $objDrawing[$k]->setWidth(120); //照片宽度
                /*设置图片要插入的单元格*/
                $objDrawing[$k]->setCoordinates('W'.$k);
                // 图片偏移距离
                $objDrawing[$k]->setOffsetX(12);
                $objDrawing[$k]->setOffsetY(12);
                $objDrawing[$k]->setWorksheet($objPHPExcel->getActiveSheet());

            }else{
                $objActSheet->setCellValue('W'.$k, $v['id_img_up']);
            }
            if($v['id_img_down']){

                $objDrawing[$k] = new \PHPExcel_Worksheet_Drawing();
                $objDrawing[$k]->setPath('.'.$v['id_img_down']);//这里拼接 . 是因为要在根目录下获取
                // 设置宽度高度
                $objDrawing[$k]->setHeight(80);//照片高度
                $objDrawing[$k]->setWidth(120); //照片宽度
                /*设置图片要插入的单元格*/
                $objDrawing[$k]->setCoordinates('X'.$k);
//                // 图片偏移距离
                $objDrawing[$k]->setOffsetX(12);
                $objDrawing[$k]->setOffsetY(12);
                $objDrawing[$k]->setWorksheet($objPHPExcel->getActiveSheet());

            }else{
                $objActSheet->setCellValue('X'.$k, $v['id_img_down']);
            }

            if($v['buchong']){
                $ids = explode(',',$v['buchong']);
                $map[] = ['id','in',$ids];
                //获取到图片信息
                $img = Db::name('images')->where($map)->field('img')->select();
                foreach ($img as $k1 => $v1){
                    $objDrawing[$k1] = new \PHPExcel_Worksheet_Drawing();
                    $objDrawing[$k1]->setPath('.'.$v1['img']);//这里拼接 . 是因为要在根目录下获取
                    // 设置宽度高度
                    $objDrawing[$k1]->setHeight(40);//照片高度
                    $objDrawing[$k1]->setWidth(120); //照片宽度
                    /*设置图片要插入的单元格*/
                    $objDrawing[$k1]->setCoordinates($cellName[23+$k1].$k);
//                // 图片偏移距离
                    $objDrawing[$k1]->setOffsetX(12);
                    $objDrawing[$k1]->setOffsetY(12);
                    $objDrawing[$k1]->setWorksheet($objPHPExcel->getActiveSheet());
                }
            }

            //            $objActSheet->setCellValue('X'.$k, '');
//            $objActSheet->setCellValue('Y'.$k, '');

            // 表格高度
            $objActSheet->getRowDimension($k)->setRowHeight(80);
        }

        $fileName = '帮扶表格';
        $date = date("Y-m-d",time());
        $fileName .= "_{$date}.xls";
        $fileName = iconv("utf-8", "gb2312", $fileName);
        //重命名表
        // $objPHPExcel->getActiveSheet()->setTitle('test');
        //设置活动单指数到第一个表,所以Excel打开这是第一个表
        $objPHPExcel->setActiveSheetIndex(0);
		ob_end_clean();
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output'); //文件通过浏览器下载

    }


}